package com.egao.common.module.workLog.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.core.exception.ParameterException;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.workLog.entity.AnnualPlan;
import com.egao.common.module.workLog.mapper.AnnualPlanMapper;
import com.egao.common.module.workLog.service.AnnualPlanService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;


/**
 *  服务实现类
 *
 * @author shu
 */
@Service
public class AnnualPlanServiceImpl extends ServiceImpl<AnnualPlanMapper, AnnualPlan> implements AnnualPlanService {

    @Override
    public PageResult<AnnualPlan> list(PageParam<AnnualPlan> page) {
        QueryWrapper<AnnualPlan> wrapper = new QueryWrapper<>();
        wrapper.eq("year",page.getPageData().get("year"));
        if(page.getPageData().get("type") != null){
            wrapper.eq("type",page.getPageData().get("type"));
        }
        if(page.getPageData().get("userDepartment") != null){
            wrapper.eq("user_department",page.getPageData().get("userDepartment"));
        }
        if (page.getPageData().get("title") != null && StringUtils.isNotBlank(page.getPageData().get("title").toString())) {
            wrapper.like("title", page.getPageData().get("title"));
        }

        wrapper.select("id", "year", "sequence", "title", "type", "user_department",
                "start_time", "end_time", "create_time", "update_time", "deleted");
        wrapper.orderByAsc("type","sequence");
        return new PageResult<>(this.page(page,wrapper).getRecords(), page.getTotal());
    }

    @Override
    public JsonResult read(Integer id) {
        if (id == null) {
            throw new ParameterException("参数对象为空");
        }

        AnnualPlan annualPlan = baseMapper.selectById(id);
        return JsonResult.ok().setData(annualPlan);
    }

    @Override
    public JsonResult add(AnnualPlan annualPlan) {
        if (annualPlan == null){
            throw new ParameterException("参数对象为空");
        }
        if(annualPlan.getYear() == null){
            throw new ParameterException("参数对象为空");
        }
        if(annualPlan.getType() == null){
            throw new ParameterException("参数对象为空");
        }
        if(annualPlan.getUserDepartment() == null){
            throw new ParameterException("参数对象为空");
        }

        baseMapper.insert(annualPlan);
        return JsonResult.ok("添加成功");
    }

    @Override
    public JsonResult update(AnnualPlan annualPlan) {
        if (annualPlan == null) {
            throw new ParameterException("参数对象为空");
        }

        if (annualPlan.getId() == null) {
            throw new ParameterException("参数对象id为空");
        }

        baseMapper.updateById(annualPlan);
        return JsonResult.ok("修改成功");
    }

    @Override
    public JsonResult delete(Integer[] ids) {
        if (ids == null) {
            throw new ParameterException("参数对象为空");
        }

        if (ids != null || ids.length > 0) {
            baseMapper.deleteBatchIds(Arrays.asList(ids));
        }
        return JsonResult.ok("删除成功");
    }

}